import axios from 'axios';

import { server_base_url } from '../config';
import { info } from './common';

export const deposit = {
    /**
     * 充值接口
     */
    async deposit(formData) {
        try {
            const token = localStorage.getItem('token');
            if (!token) throw new Error('token is null');
            const response = await axios.post(`${ server_base_url }/recharge/apply`, formData, {
                headers: { token }
            });
            const data = response.data;
            info('[ resposeData ] deposit', data);
            return { success: true, data };
        } catch (err) {
            info('[ err ] deposit', err);
            return { success: false, err };
        }
    },
    /**
     * 提现接口
     */
    async withdraw(amount) {
        try {
            const token = localStorage.getItem('token');
            if (!token) throw new Error('token is null');
            const response = await axios.post(`${ server_base_url }/withdraw/apply`, { amount }, {
                headers: { token }
            });
            const data = response.data;
            info('[ resposeData ] withdraw', data);
            return { success: true, data };
        } catch (err) {
            info('[ err ] withdraw', err);
            return { success: false, err };
        }
    },
    /**
     * 获取充值列表接口
     */
    async getDepositList() {
        try {
            const token = localStorage.getItem('token');
            if (!token) throw new Error('token is null');
            const response = await axios.post(`${ server_base_url }/recharge/list`, {}, {
                headers: { token }
            });
            const data = response.data;
            info('[ resposeData ] getDepositList', data);
            return { success: true, data };
        } catch (err) {
            info('[ err ] getDepositList', err);
            return { success: false, err };
        }
    },
    /**
     * 获取提现列表接口
     */
    async getWithdrawList() {
        try {
            const token = localStorage.getItem('token');
            if (!token) throw new Error('token is null');
            const response = await axios.post(`${ server_base_url }/withdraw/list`, {}, {
                headers: { token }
            });
            const data = response.data;
            info('[ resposeData ] getWithdrawList', data);
            return { success: true, data };
        } catch (err) {
            info('[ err ] getWithdrawList', err);
            return { success: false, err };
        }
    },
};
